import fs = require("node:fs");
import inspector = require("node:inspector/promises");
const session = new inspector.Session();
session.connect();

(async () => {
  await session.post("Profiler.enable");
  await session.post("Profiler.start");
  try {
    const sessionInner = new inspector.Session();
    sessionInner.connect();
    const result = await sessionInner.post("Runtime.evaluate", {
      expression: "2 + 2",
    });
    console.log(result);
  } catch (error) {
    console.error(error);
  }

  // Invoke business logic under measurement here...

  // some time later...
  const { profile } = await session.post("Profiler.stop");

  // Write profile to disk, upload, etc.
  fs.writeFileSync("./profile.cpuprofile", JSON.stringify(profile));
})();
// Output: { result: { type: 'number', value: 4, description: '4' } }
// TODO: to assign `data-inspector` to a HTML template
